import pandas as pd
from selenium import webdriver
from time import sleep
import urllib3
urllib3.disable_warnings()
from lxml import etree
from pandas import DataFrame as df
import os
 
class meiguanjia:
    def __init__(self):
        #User-Agent":"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;
        options = webdriver.ChromeOptions()
        options.add_argument('--User-Agent"="Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;')
        self.bro = webdriver.Chrome()
        self.bro.maximize_window()
        self.user ='18613221977'
        self.pwd = '123456'
        self.urls= 'https://vip7.meiguanjia.net/shair/?v=mgj&fromOfficial=1'
        self.num=1
        self.datas_np = []
        #会员卡
        self.huiyuanka=[]        
        #套餐
        self.taocan=[]
        #欠款
        self.qiankuan=[]
        #消费记录
        self.solecoe=[]
        #输入值1,2,3,4,5
        self.numm=6


    def auto_click(self,pa):#自动点击
        into = self.bro.find_element_by_xpath(pa)
        self.bro.execute_script('arguments[0].click();',into)
        sleep(1)
        self.bro.current_window_handle
    
    def auto_send(self,pa,sa):#自动输入
        self.bro.find_element_by_xpath(pa).clear()
        sleep(1)
        self.bro.find_element_by_xpath(pa).send_keys(sa)
        sleep(1)

    def login_index(self):#登录
        self.bro.get(self.urls)
        sleep(1)
        self.bro.current_window_handle
        sleep(1)
        #账号
        self.auto_send('//*[@id="username"]',self.user)
        #密码
        self.auto_send('//*[@id="password"]',self.pwd)
        #登录
        self.auto_click('//*[@id="login"]')
        sleep(4)
        self.into_index()

    def into_index(self):#进入主页面
        #管理层/           
        self.auto_click('/html/body/div[2]/ul/li[1]/a')
        #会员管理
        self.auto_click('/html/body/div[2]/div[1]/ul/li[3]/a')
        #进入frame
        # frame = self.bro.find_element_by_xpath('//*[@id="stockIframe"]/iframe')
        # self.bro.switch_to.frame(frame)
        # sleep(1)<shair/components/customerRelation/index.html#/index
        self.bro.get('https://vip7.meiguanjia.net/shair/components/customerRelation/index.html#/index')
        self.bro.current_window_handle
        sleep(1)
        #顾客查询 self.auto_click('')
        self.auto_click('//*[@id="app"]/div[1]/div[1]/nav/ul/li[2]/a')
        #高级搜索
        self.auto_click('//*[@id="advanced_query"]/div[1]/div[2]/button')
        #点出下拉
        self.auto_click('/html/body/div[2]/div/div[2]/form/div[1]/div[1]/div[2]/div[1]/div[1]/div/div/div[2]/span/span/i')
        #选择店铺
        self.auto_click('/html/body/div[4]/div[1]/div[1]/ul/li[3]')
        #搜索
        self.auto_click('/html/body/div[2]/div/div[2]/form/div[2]/button')
        sleep(1) 
        #获取基本信息
        # self.into_title()
        # self.into_title_excel()
        #获取详情信息
        self.into_title1()
        self.into_title_excel1()

    def into_title1(self):
        #进入输入框查询每个会员
        datas = self.read_excel()
        num = datas.shape[0]

        for i in range(0,num,1):
            srname = datas.loc[i,'姓名']
            phone = datas.loc[i,'电话']
            sleep(2)
            self.auto_send('//*[@id="app"]/div[1]/div[2]/div/div[1]/form/div[1]/div/div/input',int(phone))
            #点击搜索
            self.auto_click('//*[@id="app"]/div[1]/div[2]/div/div[1]/form/div[1]/div/div/div/button')
            #点击人名称
            self.auto_click('//*[@id="app"]/div[1]/div[2]/div/div[2]/div[1]/div[2]/div/div[4]/div[2]/table/tbody/tr/td[2]/div/span/a')
            #会员卡
            self.huiyuankas(srname,phone)
            #套餐
            self.taocans(srname,phone)
            #欠款
            # self.qiankuans(srname,phone)
            #消费记录
            self.solecos(srname,phone)
            self.bro.back()
    
    def tohtml(self):#变成html
        page = self.bro.page_source
        st = etree.HTML(page)
        return st

    def huiyuankas(self,srname,phone):#会员卡
        huiyuan =[]
        sleep(1)
        huiyuan.append(srname)
        sleep(1)
        huiyuan.append(phone)
        st = self.tohtml()
        try:
            birthday = st.xpath('//*[@id="app"]/div[1]/div[2]/div/div[1]/div/div[1]/ul/li[4]/span//text()')[0]
            huiyuan.append(birthday)
            card = st.xpath('//*[@id="app"]/div[1]/div[2]/div/div[1]/div/div[3]/div[2]/div/div/div[1]/div[3]/div[1]/span/b//text()')[0]
            huiyuan.append(card)
        except Exception as e:
            print("没有会员卡>>>",e)  
        try:      
            cardtype = st.xpath('//*[@id="app"]/div[1]/div[2]/div/div[1]/div/div[3]/div[2]/div/div/div[1]/div[2]/p//text()')[0]
            huiyuan.append(cardtype)
        except Exception as e:
            print("没有卡类型>>>>",e)
        try:
            zhekou = st.xpath('//*[@id="app"]/div[1]/div[2]/div/div[1]/div/div[3]/div[2]/div/div/div[1]/div[2]/div/div/span[1]/b//text()')[0]
            huiyuan.append(zhekou)
        except Exception as e:
            print("没有折扣>>>",e)
        try:    
            yue = st.xpath('//*[@id="app"]/div[1]/div[2]/div/div[1]/div/div[3]/div[2]/div/div/div[1]/div[2]/div/div/span[2]/b//text()')[0]
            huiyuan.append(yue)
        except Exception as e:
            print("没有余娥>>>",e)
        try:
            send = st.xpath('//*[@id="app"]/div[1]/div[2]/div/div[1]/div/div[3]/div[2]/div/div/div[1]/div[2]/div/div/span[3]/b//text()')[0]
            huiyuan.append(send)
        except Exception as e:
            print("没有>>>",e)
        try:
            lastdate= st.xpath('//*[@id="app"]/div[1]/div[2]/div/div[1]/div/div[3]/div[2]/div/div/div[1]/div[3]/div[2]/span/b//text()')[0]
            huiyuan.append(lastdate)
        except Exception as e:
            print("没有>>>",e)
        try:
            startdate = st.xpath('//*[@id="app"]/div[1]/div[2]/div/div[1]/div/div[3]/div[2]/div/div/div[1]/div[3]/div[3]/span[1]//text()')[0]
            huiyuan.append(startdate)
        except Exception as e:
            print("没有>>>",e)   
        self.huiyuanka.append(huiyuan)
        sleep(1)
       
    
    def taocans(self,srname,phone):#套餐
        st = self.tohtml()
        try:
            taocanlist = st.xpath('//*[@id="app"]/div[1]/div[2]/div/div[1]/div/div[4]/div[2]//div[@class="list"]')
            for taocanli in taocanlist:
                taocan = []
                sleep(1)
                taocan.append(srname)
                sleep(1)
                taocan.append(phone)
                tao = taocanli.xpath('string(.)')
                taocan.append(tao)
                print(tao)
                self.taocan.append(taocan)
        except Exception as e:
             print("应该没有套餐>>>",e)       
       
    def qiankuans(self,srname,phone):#欠款
        qiankuan=[]
        qiankuan.append(srname)
        self.auto_click('//*[@id="app"]/div[1]/div[1]/ul/li[4]')
        qiankuan.append(phone)

        self.bro.back()

    def solecos(self,srname,phone):#消费记录
        self.auto_click('//*[@id="app"]/div[1]/div[1]/ul/li[7]')
        self.auto_click('//*[@id="app"]/div[1]/div[2]/div/div[1]/form/div[4]/div/div/div[1]/span/span/i')
        self.auto_click('/html/body/div[2]/div[1]/div[1]/ul/li[2]')
        self.auto_click('//*[@id="app"]/div[1]/div[2]/div/div[1]/form/div[5]/div/button/span')
        st = self.tohtml()
        try:
            lines=st.xpath('//*[@id="app"]/div[1]/div[2]/div/div[2]/div/div[2]/div/div[3]/table//tr')
            for lin in lines:
                soleco =[]
                sleep(1)
                soleco.append(srname)
                sleep(1)
                soleco.append(phone)
                soletype=lin.xpath('.//td[2]/div//text()')[0]
                soleco.append(soletype)
                soleMoney = lin.xpath('.//td[3]/div//text()')[0]
                soleco.append(soleMoney)
                solemendian=lin.xpath('.//td[4]/div//text()')[0]
                soleco.append(solemendian)
                soletime = lin.xpath('.//td[5]/div//text()')[0]
                soleco.append(soletime)
                solestatus =lin.xpath('.//td[6]/div//text()')[0]
                soleco.append(solestatus)
                print(soleco)
                self.solecoe.append(soleco)
        except Exception as e:
            print("没有消费记录>>>",e)
        self.bro.back()

    def read_excel(self):
        datas = pd.read_excel(os.path.abspath(os.path.join(os.getcwd()+str(self.numm)+'.xlsx')),sheet_name="Sheet1")
        return datas    

    def into_title(self):#0
        #进入获取每个会员信息
        page = self.bro.page_source
        sleep(1)
        st = etree.HTML(page)
        sleep(1)
        #获取会员记录
        list_trs = st.xpath('//*[@id="app"]/div[1]/div[2]/div/div[2]/div[1]/div[2]/div/div[4]/div[2]/table//tr')
        sleep(1)
        for trs in list_trs:
            np=[]
            srname = trs.xpath('.//td[2]/div/span/a//text()')[0]
            srname.replace('\r','').replace('\n','')
            np.append(srname)
            sleep(1)
            #//*[@id="app"]/div[1]/div[2]/div/div[2]/div[1]/div[2]/div/div[3]/table/tbody/tr[1]/td[5]/div
            phone = trs.xpath('.//td[5]/div//text()')[0]
            np.append(phone)
            sleep(1)
            self.datas_np.append(np)
            print(srname,phone)
        #翻页
        self.auto_click('//*[@id="app"]/div[1]/div[2]/div/div[2]/div[2]/button[2]')
        sleep(1)
        self.num=self.num+1
        if self.num > 20:
            return
        self.into_title()
        sleep(1)

    def into_title_excel(self):#存入excel
        datas = df(self.datas_np)
        datas.to_excel(os.path.abspath(os.path.dirname(__file__))+'base.xlsx',index=False)

    def into_title_excel1(self):#存入excel 详细信息
        datas = df(self.huiyuanka)
        datas.to_excel(os.path.abspath(os.path.join(os.getcwd()+'会员卡'+str(self.numm)+'.xlsx')))
        datas = df(self.taocan)
        datas.to_excel(os.path.abspath(os.path.join(os.getcwd()+'套餐'+str(self.numm)+'.xlsx')))
        datas = df(self.qiankuan)
        # datas.to_excel(os.path.abspath(os.path.dirname(__file__))+'欠款.xlsx')
        datas = df(self.solecoe)
        datas.to_excel(os.path.abspath(os.path.join(os.getcwd()+'消费记录'+str(self.numm)+'.xlsx')))

   

    def run(self):
        self.login_index()

if __name__ == "__main__":
    meiguanjia().run()




    

